
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ScrewTurn.Wiki.PluginFramework;
using System.Text;

namespace ScrewTurn.Wiki {

	public partial class Print : BasePage {

		PageInfo page = null;
		PageContent content = null;

		protected void Page_Load(object sender, EventArgs e) {
			page = Pages.Instance.FindPage(Request["Page"]);
			if(page == null) Response.Redirect(Settings.DefaultPage + Settings.PageExtension);

			content = Content.GetPageContent(page, true);

			Page.Title = content.Title + " - " + Settings.WikiTitle;

			PrintContent();
		}

		public void PrintContent() {
			StringBuilder sb = new StringBuilder();
			if(Request["Discuss"] == null) {
				sb.Append(@"<h1 class=""pagetitle"">" + content.Title + "</h1>");
				sb.Append(Content.GetFormattedPageContent(page, true));
			}
			else {
				sb.Append(@"<h1 class=""pagetitle"">" + content.Title + " - Discussion</h1>");
				PrintDiscussion(sb);
			}
			lblContent.Text = sb.ToString();
		}

		private void PrintDiscussion(StringBuilder sb) {
			List<Message> messages = new List<Message>(Pages.Instance.GetPageMessages(page));
			if(messages.Count == 0) {
				sb.Append("<i>");
				sb.Append(Resources.Messages.NoMessages);
				sb.Append("</i>");
			}
			else PrintSubtree(messages, null, sb);
		}

		/// <summary>
		/// Prints a subtree of Messages depth-first.
		/// </summary>
		/// <param name="messages">The Messages.</param>
		private void PrintSubtree(List<Message> messages, Message parent, StringBuilder sb) {
			for(int i = 0; i < messages.Count; i++) {
				sb.Append(@"<div" + (parent != null ? @" class=""messagecontainer""" : @" class=""rootmessagecontainer""") + ">");
				PrintMessage(messages[i], parent, sb);
				PrintSubtree(messages[i].Replies, messages[i], sb);
				sb.Append("</div>");
			}
		}

		private void PrintMessage(Message message, Message parent, StringBuilder sb) {
			// Header
			sb.Append(@"<div class=""messageheader"">");
			string shift = "";
			if(Request.Cookies[Settings.CultureCookieName] != null) shift = Request.Cookies[Settings.CultureCookieName]["T"];
			sb.Append(Tools.AlignWithPreferences(message.DateTime, shift).ToString(Settings.DateTimeFormat));
			sb.Append(" ");
			sb.Append(Resources.Messages.By);
			sb.Append(" ");
			sb.Append(Users.Instance.UserLink(message.Username));

			// Subject
			if(message.Subject.Length > 0) {
				sb.Append(@"<br /><span class=""messagesubject"">");
				sb.Append(message.Subject);
				sb.Append("</span>");
			}

			sb.Append("</div>");

			// Body
			sb.Append(@"<div class=""messagebody"">");
			sb.Append(FormattingPipeline.FormatWithPhase1And2(message.Body, page));
			sb.Append("</div>");
		}

	}

}
